package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.KolejAssembler;
import cz.muni.fi.pa036.kiss.Entity.KolejDTO;
import cz.muni.fi.pa036.kiss.Entity.StudentAssembler;
import cz.muni.fi.pa036.kiss.Entity.StudentDTO;
import cz.muni.fi.pa036.kiss.Entity.VybaveniPokojeAssembler;
import cz.muni.fi.pa036.kiss.Entity.VybaveniStudenta;
import cz.muni.fi.pa036.kiss.Entity.VybaveniStudentaAssembler;
import cz.muni.fi.pa036.kiss.Entity.VybaveniStudentaDTO;
import cz.muni.fi.pa036.kiss.dao.VybaveniPokojeDAO;
import cz.muni.fi.pa036.kiss.dao.VybaveniStudentaDAO;
import cz.muni.fi.pa036.kiss.service.VybaveniStudentaService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class VybaveniStudentaServiceImpl implements VybaveniStudentaService {

    @Autowired
    private VybaveniStudentaAssembler assembler;
    @Autowired
    private VybaveniStudentaDAO vybaveniDao;
    @Autowired
    private KolejAssembler kolejAss;
    @Autowired
    private StudentAssembler studentAss;
         

    @Transactional
    @Override
    public void create(VybaveniStudentaDTO vybaveni) {
        if (vybaveni == null) {
            throw new IllegalArgumentException("vybaveni is null: ");
        }
        if (vybaveni.getId() != null) {
            throw new IllegalArgumentException("id is not null");
        }
        VybaveniStudenta pok = assembler.createEntityfromDTO(vybaveni);
        vybaveniDao.create(pok);
        vybaveni.setId(pok.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public VybaveniStudentaDTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        VybaveniStudentaDTO vyb = assembler.createDTOfromEntity(vybaveniDao.get(id));
        if (vyb == null) {
            return null;
        } else {
            return vyb;
        }
    }

    @Transactional
    @Override
    public void update(VybaveniStudentaDTO vybaveni) {
        if (vybaveni == null) {
            throw new IllegalArgumentException("vybaveni is null");
        }
        vybaveniDao.update(assembler.createEntityfromDTO(vybaveni));
    }
    
    @Transactional
    @Override
    public void remove(VybaveniStudentaDTO vybaveni) {
        if (vybaveni == null) {
            throw new IllegalArgumentException("vybaveni is null");
        }
        vybaveniDao.remove(assembler.createEntityfromDTO(vybaveni));
    }

    @Override
    public List<VybaveniStudentaDTO> findAll() {
        List<VybaveniStudenta> all = vybaveniDao.findAll();
        List<VybaveniStudentaDTO> allDto = new ArrayList<VybaveniStudentaDTO>();
        for (VybaveniStudenta vybaveni : all) {
            allDto.add(assembler.createDTOfromEntity(vybaveni));
        }
        return allDto;
    }

    @Override
    public List<VybaveniStudentaDTO> findByKolej(KolejDTO kolej) {
        if (kolej == null) {
            throw new IllegalArgumentException("kolej is null");
        }
        List<VybaveniStudenta> all = vybaveniDao.findByKolej(kolejAss.createEntityfromDTO(kolej));
        List<VybaveniStudentaDTO> allDto = new ArrayList<VybaveniStudentaDTO>();
        for (VybaveniStudenta vybaveniStudenta : all) {
            allDto.add(assembler.createDTOfromEntity(vybaveniStudenta));
        }
        return allDto;
    }

    @Override
    public List<VybaveniStudentaDTO> findByStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        List<VybaveniStudenta> all = vybaveniDao.findByStudent(studentAss.createEntityfromDTO(student));
        List<VybaveniStudentaDTO> allDto = new ArrayList<VybaveniStudentaDTO>();
        for (VybaveniStudenta vybaveniStudenta : all) {
            allDto.add(assembler.createDTOfromEntity(vybaveniStudenta));
        }
        return allDto;
    }
}
